package com.ruoyi.system.module.appliction.service.serviceImpl;



import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.domain.SysRoleMenu;
import com.ruoyi.system.mapper.SysRoleMenuMapper;
import com.ruoyi.system.mapper.SysUserRoleMapper;
import com.ruoyi.system.module.appliction.entity.Application;
import com.ruoyi.system.module.appliction.entity.ApplicationCounts;
import com.ruoyi.system.module.appliction.mapper.ApplicationCountMapper;
import com.ruoyi.system.module.appliction.mapper.ApplicationMapper;
import com.ruoyi.system.module.appliction.service.ApplicationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 菜单 业务层处理
 *
 * @author yinhe
 */
@Service
public class ApplicationServiceImpl implements ApplicationService
{
    public static final String PREMISSION_STRING = "perms[\"{0}\"]";

    @Autowired
    private ApplicationMapper menuMapper;

    @Autowired
    private ApplicationCountMapper countMapper;

    @Resource
    private SysRoleMenuMapper roleMenuMapper;

    @Resource
    private SysUserRoleMapper sysUserRoleMapper;




    /**
     * 查询系统菜单列表
     *
     * @param menu 菜单信息
     * @return 菜单列表
     */
    @Override
    public List<Application> selectMenuList(Application menu, Long userId)
    {
       List<Application> menuList = null;


        ArrayList<Application> list1 = new ArrayList<>();
        // 管理员显示所有菜单信息
        if (SysUser.isAdmin(userId))
        {
            menuList = menuMapper.selectMenuList(menu);
            return menuList;
        }
        else
        {
            Long roleId = sysUserRoleMapper.selectByUserID(userId);
            menuList = menuMapper.selectMenuList(menu);
            List<SysRoleMenu> roleList = roleMenuMapper.selectRole(roleId);
            list1 = (ArrayList<Application>) menuList.stream()
                    .filter(menu2 -> roleList.stream()
                            .anyMatch(role -> role.getMenuId().longValue() == menu2.getMenuId().longValue()))
                    .collect(Collectors.toList());
//            Long roleId = sysUserRoleMapper.selectByUserID(userId);
//            menuList = menuMapper.selectMenuList(menu);
//            //根据roleId查询用户可以访问的用户列表
//            List<SysRoleMenu>  roleList =  menuMapper.selectRole(roleId);
//            Long tempId  = 0l;
//            for (Application b:menuList){
//                for (SysRoleMenu a:roleList){
//                    tempId = a.getMenuId();
//                    if (b.getMenuId().longValue()==tempId.longValue()){
//                        list1.add(b);
//                    }
//                }
//
//            }
        }


        return list1;
    }



    /**
     *
     * @param menuId 根据Id查询消息
     * @return
     */

    @Override
    public List<Application> selectMenuById(Long menuId)
    {

        return menuMapper.selectMenuById(menuId);
    }




    /**
     *
     * 获取常规应用
     */
    @Override
    public List<Application> getNormalList() {

        return menuMapper.selectNormalList();
    }

    @Override
    public Application selectMenuByIdA(Long menuId) {
        return menuMapper.selectByIdA(menuId);
    }

    /**
     *
     *
     * 根据parentId查询他父亲的menuId
     */
    @Override
    public Application selectByMenuId(Long parentId) {
        return menuMapper.selectByMenuId(parentId);
    }
    @Override
    public int selectMenuByParentId(Long parentId) {
        return menuMapper.selectByParentId(parentId);
    }

    @Override
    public int selectFrequency(Long userId, String username, Long menuId, String menuName) {
        //根据menuID和userID查询用户点击应用的列表
//        System.out.println(userId+"888");
        List<ApplicationCounts> result =   countMapper.selectSysApplicationCountsById(userId,menuId);


        ApplicationCounts sysApplicationCounts = new ApplicationCounts();
        ApplicationCounts sysApplicationCounts1 = new ApplicationCounts();
        Integer rows = 0;

        sysApplicationCounts.setUserId(userId);
        sysApplicationCounts.setUsername(username);
        sysApplicationCounts.setMenuId(menuId);
        sysApplicationCounts.setMenuname(menuName);
        sysApplicationCounts.setFrequency(1);

        if (result.size()==0){
            int  Frequency = 1;
            rows =     countMapper.insertSysApplicationCounts(sysApplicationCounts);
            //   System.out.println(rows+"999");
        }
        else {

            ArrayList<ApplicationCounts> list = new ArrayList<>();
            //循环list获取次数，将次数+1

            for (ApplicationCounts a:result){
                Integer Frequency =  a.getFrequency();
                Integer   id  =  a.getId();

                sysApplicationCounts1.setFrequency(Frequency+1);
                sysApplicationCounts1.setId(id);
                rows =   countMapper.updateFrequency(sysApplicationCounts1);
            }
        }
        return rows;
    }


    /**
     *
     * 应用管理使用日志
     */
//    @Override
//    public int selectFrequency(Long userId, String username, Long menuId, String menuName) {
//        //根据menuID和userID查询用户点击应用的列表
////        System.out.println(userId+"888");
//        List<SysApplicationCounts> result =   countMapper.selectSysApplicationCountsById(userId,menuId);
//
//
//        SysApplicationCounts sysApplicationCounts = new SysApplicationCounts();
//        SysApplicationCounts sysApplicationCounts1 = new SysApplicationCounts();
//        Integer rows = 0;
//
//        sysApplicationCounts.setUserId(userId);
//        sysApplicationCounts.setUsername(username);
//        sysApplicationCounts.setMenuId(menuId);
//        sysApplicationCounts.setMenuname(menuName);
//        sysApplicationCounts.setFrequency(1);
//
//        if (result.size()==0){
//            int  Frequency = 1;
//         rows =     countMapper.insertSysApplicationCounts(sysApplicationCounts);
//            //   System.out.println(rows+"999");
//        }
//        else {
//
//            ArrayList<ApplicationCounts> list = new ArrayList<>();
//            //循环list获取次数，将次数+1
//
//            for (SysApplicationCounts a:result){
//                Integer Frequency =  a.getFrequency();
//                Integer   id  =  a.getId();
//
//                sysApplicationCounts1.setFrequency(Frequency+1);
//                sysApplicationCounts1.setId(id);
//               rows =   countMapper.updateFrequency(sysApplicationCounts1);
//            }
//        }
//        return rows;
//    }
//
//    @Override
//    public List<SysApplicationCounts> searchNumber(SysApplicationCounts sysApplicationCounts) {
//        return countMapper.searchNumber(sysApplicationCounts);
//    }

    @Override
    public void selectRole(Long roleId) {


    }

    //查询常规应用
    @Override
    public List<Application> selectByNormal() {
    List<Application> normalmenu = menuMapper.selectNormalList();
    List<Application> list = normalmenu.stream()
            .filter(menu -> menu.getMenuType().equals("C"))
            .map(menu -> {
                Application parentMenu = menuMapper.selectByMenuId(menu.getParentId());
                if (parentMenu != null) {
                    String parentPath = parentMenu.getPath();
                    String path = parentPath + "/" + menu.getPath();
                    menu.setPath(path);
                    return menu;
                } else {
                    return null;
                }
            })
            .filter(Objects::nonNull)
            .collect(Collectors.toList());
        return list;
    }


}

